<script setup>
import { ElMessage } from 'element-plus';
import { onMounted, ref } from 'vue';
import dayjs from 'dayjs'

// 每页多少条数据
const pageSize = ref(10)
// 当前是第几页
const currentPage = ref(1)
// 量表选择数据 - 分为8大类
const scales = ref([
  // 1.1 心理健康类
  { value: 1, key: 1, label: '抑郁自评量表（SDS）', group: '心理健康' },
  { value: 2, key: 2, label: '焦虑自评量表（SAS）', group: '心理健康' },
  { value: 3, key: 3, label: '青少年生活事件量表', group: '心理健康' },
  { value: 4, key: 4, label: 'SCL-90症状自评量表', group: '心理健康' },
  { value: 5, key: 5, label: '情绪自评量表（DASS-21）', group: '心理健康' },
  { value: 6, key: 6, label: '青春期性心理健康量表', group: '心理健康' },
  
  // 1.2 职业选择类
  { value: 7, key: 7, label: 'MBTI性格类型测试问卷', group: '职业选择' },
  { value: 8, key: 8, label: '霍兰德职业倾向测验量表', group: '职业选择' },
  
  // 1.3 学习心理类
  { value: 9, key: 9, label: '认知灵活性问卷', group: '学习心理' },
  { value: 10, key: 10, label: '学习沉醉感量表', group: '学习心理' },
  { value: 11, key: 11, label: '时间管理自我监控量表', group: '学习心理' },
  { value: 12, key: 12, label: '创造性倾向问卷', group: '学习心理' },
  { value: 13, key: 13, label: '考试心理和行为问题症状自评量表', group: '学习心理' },
  { value: 14, key: 14, label: '学习倦怠量表', group: '学习心理' },
  
  // 1.4 学校文化类
  { value: 15, key: 15, label: '师生关系量表', group: '学校文化' },
  { value: 16, key: 16, label: '青少年学生生活满意度量表', group: '学校文化' },
  
  // 1.5 恋爱关系类
  { value: 17, key: 17, label: '恋爱心理压力源量表', group: '恋爱关系' },
  { value: 18, key: 18, label: '斯腾伯格爱情量表', group: '恋爱关系' },
  
  // 1.6 人格特质类
  { value: 19, key: 19, label: '心理韧性量表', group: '人格特质' },
  { value: 20, key: 20, label: '自我认同感量表', group: '人格特质' },
  { value: 21, key: 21, label: '大五人格量表简化版', group: '人格特质' },
  { value: 22, key: 22, label: '艾森克人格问卷（EPQ）', group: '人格特质' },
  { value: 23, key: 23, label: '情商测试量表', group: '人格特质' },
  { value: 24, key: 24, label: '卡特尔16种性格因素人格测验', group: '人格特质' },
  { value: 25, key: 25, label: 'Rosenberg自尊量表', group: '人格特质' },
  
  // 1.7 家庭关系类
  { value: 26, key: 26, label: '家庭亲合量表', group: '家庭关系' },
  { value: 27, key: 27, label: '社会支持评定量表', group: '家庭关系' },
  { value: 28, key: 28, label: '父母教养方式问卷', group: '家庭关系' },
  { value: 29, key: 29, label: '双元孝道信念量表', group: '家庭关系' },
  
  // 1.8 人际关系类
  { value: 30, key: 30, label: '人际关系综合诊断量表', group: '人际关系' },
  { value: 31, key: 31, label: '社交焦虑量表', group: '人际关系' },
  { value: 32, key: 32, label: '人际信任量表（ITS）', group: '人际关系' },
])
// 已选量表的 key 数组
const rightValue = ref([])

// 将量表数据按分类进行分组
const getGroupedScales = () => {
  const grouped = {};
  scales.value.forEach(scale => {
    if (!grouped[scale.group]) {
      grouped[scale.group] = [];
    }
    grouped[scale.group].push(scale);
  });
  return grouped;
};
// 状态选择数据
const conditions = ref([
  {
    value: 1,
    label: '全部',
  },
  {
    value: 2,
    label: '已完成',
  },
  {
    value: 3,
    label: '未完成',
  }
])
// 表格数据 - 关联河南工业和信息化职业学院四级结构
const tableData = ref([
  { id: 1,
    title: '计算机应用技术专业心理评测',
    name: '李明',
    account: 'ZW20250901',
    scale: 1, // 抑郁自评量表（SDS）- 心理健康类
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2025级',
    class: '1班'
  },
  { id: 2,
    title: '计算机应用技术专业心理评测',
    name: '李娜',
    account: 'LN20250902',
    scale: 2, // 焦虑自评量表（SAS）- 心理健康类
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2025级',
    class: '1班'
  },
  { id: 3,
    title: '计算机应用技术专业心理评测',
    name: '王强',
    account: 'WQ20250903',
    scale: 4, // SCL-90症状自评量表 - 心理健康类
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2025级',
    class: '2班'
  },
  { id: 4,
    title: '计算机应用技术专业心理评测',
    name: '孙晓',
    account: 'SX20250913',
    scale: 30, // 人际关系综合诊断量表 - 人际关系类
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2025级',
    class: '3班'
  },
  { id: 5,
    title: '计算机应用技术专业心理评测',
    name: '刘伟',
    account: 'LW20250917',
    scale: 1, // 抑郁自评量表（SDS）- 心理健康类
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-18 15:30:00',
    complishTime: '2025-09-18 15:50:00',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2024级',
    class: '1班'
  },
  { id: 6,
    title: '计算机应用技术专业心理评测',
    name: '王浩然',
    account: 'ZS20240901',
    scale: 7, // MBTI性格类型测试问卷 - 职业选择类
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2024级',
    class: '2班'
  },
  { id: 7,
    title: '计算机应用技术专业心理评测',
    name: '赵阳',
    account: 'LS20240902',
    scale: 8, // 霍兰德职业倾向测验量表 - 职业选择类
    report: 0,
    eva: '...',
    condition: 3,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2024级',
    class: '3班'
  },
  { id: 8,
    title: '计算机应用技术专业心理评测',
    name: '孙磊',
    account: 'WW20230901',
    scale: 9, // 认知灵活性问卷 - 学习心理类
    report: 0,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2023级',
    class: '1班'
  },
  { id: 9,
    title: '计算机应用技术专业心理评测',
    name: '吴凡',
    account: 'ZL20230902',
    scale: 10, // 学习沉醉感量表 - 学习心理类
    report: 0,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2023级',
    class: '2班'
  },
  { id: 10,
    title: '计算机应用技术专业心理评测',
    name: '高远',
    account: 'SQ20230903',
    scale: 17, // 恋爱心理压力源量表 - 恋爱关系类
    report: 0,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2023级',
    class: '3班'
  },
  { id: 11,
    title: '计算机应用技术专业心理评测',
    name: '周八',
    account: 'ZB20220901',
    scale: 19, // 心理韧性量表 - 人格特质类
    report: 1,
    eva: '...',
    condition: 3,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2022级',
    class: '1班'
  },
  { id: 12,
    title: '计算机应用技术专业心理评测',
    name: '潘乐乐',
    account: 'WJ20220902',
    scale: 26, // 家庭亲合量表 - 家庭关系类
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2022级',
    class: '2班'
  },
  { id: 13,
    title: '计算机应用技术专业心理评测',
    name: '董晨曦',
    account: 'ZS20220903',
    scale: 30, // 人际关系综合诊断量表 - 人际关系类
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-17 15:12:45',
    complishTime: '2025-09-17 15:12:50',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2022级',
    class: '3班'
  },
  { id: 14,
    title: '班级心理评测',
    name: '李小华',
    account: 'LX20250914',
    scale: 1,
    report: 0,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-18 09:30:00',
    complishTime: '2025-09-18 09:45:00',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2025级',
    class: '1班'
  },
  { id: 15,
    title: '班级心理评测',
    name: '王小明',
    account: 'WM20250915',
    scale: 1,
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-18 10:00:00',
    complishTime: '2025-09-18 10:20:00',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2025级',
    class: '2班'
  },
  { id: 16,
    title: '班级心理评测',
    name: '张丽',
    account: 'ZL20250916',
    scale: 1,
    report: 0,
    eva: '...',
    condition: 3,
    createdTime: '2025-09-18 14:00:00',
    complishTime: '',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2025级',
    class: '3班'
  },
  { id: 17,
    title: '班级心理评测',
    name: '陈静',
    account: 'CJ20250918',
    scale: 1,
    report: 0,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-19 09:00:00',
    complishTime: '2025-09-19 09:30:00',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2024级',
    class: '1班'
  },
  { id: 18,
    title: '班级心理评测',
    name: '周杰',
    account: 'ZJ20250919',
    scale: 1,
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-19 10:00:00',
    complishTime: '2025-09-19 10:25:00',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2024级',
    class: '2班'
  },
  { id: 19,
    title: '班级心理评测',
    name: '吴敏',
    account: 'WM20250920',
    scale: 1,
    report: 0,
    eva: '...',
    condition: 3,
    createdTime: '2025-09-19 14:30:00',
    complishTime: '',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2024级',
    class: '3班'
  },
  { id: 20,
    title: '新生心理评测',
    name: '曹宇航',
    account: 'ZW20250901',
    scale: 2, // 焦虑自评量表（SAS）- 心理健康类
    report: 1,
    eva: '...',
    condition: 2,
    createdTime: '2025-09-18 09:30:00',
    complishTime: '2025-09-18 09:45:00',
    school: '河南工业和信息化职业学院',
    major: '计算机应用技术',
    grade: '2025级',
    class: '1班'
  },
])
const tableDataCopy = ref([])
// 当前页表格数据
const currentPageTableData = ref([])
// 搜索信息
const searchInfo = ref({ title: '', name: '', account: '', scale: '', condition: '' })
// 添加信息
const addInfo = ref({ title: '', name: '', account: '', scale: '', report: '', eva: '...' })
// 用于控制添加评测的对话框
const addDialogVisible = ref(false)
// 控制添加评测的内部对话框
const innerAddVisible = ref(false)
// 用于控制删除评测的对话框
const deleteDialogVisible = ref(false)
// 用于控制修改评测的对话框
const editDialogVisible = ref(false)
// 学校分类 - 河南工业和信息化职业学院四级结构
const schoolData = ref([
  {
    label: '河南工业和信息化职业学院',
    children: [
      {
        label: '计算机应用技术',
        children: [
          {
            label: '2025级',
            children: [
              { label: '1班' },
              { label: '2班' },
              { label: '3班' },
            ],
          },
          {
            label: '2024级',
            children: [
              { label: '1班' },
              { label: '2班' },
              { label: '3班' },
            ],
          },
          {
            label: '2023级',
            children: [
              { label: '1班' },
              { label: '2班' },
              { label: '3班' },
            ],
          },
          {
            label: '2022级',
            children: [
              { label: '1班' },
              { label: '2班' },
              { label: '3班' },
            ],
          },
        ],
      },
    ],
  }
])
// 内部表格输入用户名或者账号
const inputNameOrAccount = ref('')
// 用户数据 - 关联河南工业和信息化职业学院四级结构
const users = ref([
  { name: "李明", account: "ZW20250901", major: "计算机应用技术", grade: "2025级", class: "1班" },
  { name: "李娜", account: "LN20250902", major: "计算机应用技术", grade: "2025级", class: "1班" },
  { name: "王强", account: "WQ20250903", major: "计算机应用技术", grade: "2025级", class: "2班" },
  { name: "孙晓", account: "SX20250913", major: "计算机应用技术", grade: "2025级", class: "3班" },
  { name: "刘伟", account: "LW20250917", major: "计算机应用技术", grade: "2024级", class: "1班" },
  { name: "张三", account: "ZS20240901", major: "计算机应用技术", grade: "2024级", class: "2班" },
  { name: "李四", account: "LS20240902", major: "计算机应用技术", grade: "2024级", class: "3班" },
  { name: "王五", account: "WW20230901", major: "计算机应用技术", grade: "2023级", class: "1班" },
  { name: "赵六", account: "ZL20230902", major: "计算机应用技术", grade: "2023级", class: "2班" },
  { name: "孙七", account: "SQ20230903", major: "计算机应用技术", grade: "2023级", class: "3班" },
  { name: "周八", account: "ZB20220901", major: "计算机应用技术", grade: "2022级", class: "1班" },
  { name: "吴九", account: "WJ20220902", major: "计算机应用技术", grade: "2022级", class: "2班" },
  { name: "郑十", account: "ZS20220903", major: "计算机应用技术", grade: "2022级", class: "3班" }
])
// 内部表格搜索结果数据
const searchResultUsers = ref([])
// 内部表格已选择数据
const selectUsers = ref([])
// 内部表格对象
const multipleTableRef = ref()
// 记录删除的数据的id
const deleteId = ref()
// 编辑信息
const editInfo = ref({id: '', title: '', condition: '', account: '', scale: '', report: ''})
// 当前选中的学校节点
const selectedNode = ref(null)
// 点击分类节点 - 实现学生列表联动（支持四级结构：学校->专业->年级->班级）
const handleNodeClick = (data) => { 
  selectedNode.value = data
  
  // 根据点击的节点类型筛选学生
  // 重置搜索关键词
  inputNameOrAccount.value = ''
  
  // 判断节点层级：学校 -> 专业 -> 年级 -> 班级
  if (data.children) {
    // 如果是学校节点
    if (data.label === '河南工业和信息化职业学院') {
      // 如果点击的是顶级学校节点，显示所有学生
      searchResultUsers.value = users.value
    } else if (data.label === '计算机应用技术') {
      // 如果点击的是专业节点，显示该专业的所有学生
      searchResultUsers.value = users.value.filter(user => user.major === data.label)
    } else {
      // 如果点击的是年级节点，显示该年级的所有学生
      searchResultUsers.value = users.value.filter(user => user.grade === data.label)
    }
  } else {
    // 如果是班级节点（没有子节点），需要向上查找年级和专业信息
    // 找到当前节点的父节点（年级）
    const gradeNode = findParentNode(schoolData.value, data)
    if (gradeNode) {
      // 找到年级的父节点（专业）
      const majorNode = findParentNode(schoolData.value, gradeNode)
      if (majorNode) {
        // 显示该专业该年级该班级的学生
        searchResultUsers.value = users.value.filter(user => 
          user.major === majorNode.label && 
          user.grade === gradeNode.label && 
          user.class === data.label
        )
      }
    }
  }
}

// 辅助函数：查找节点的父节点
const findParentNode = (nodes, targetNode) => {
  for (const node of nodes) {
    if (node.children) {
      if (node.children.some(child => child.label === targetNode.label)) {
        return node
      }
      const found = findParentNode(node.children, targetNode)
      if (found) {
        return found
      }
    }
  }
  return null
}
// 搜索 - 模糊搜索实现
const search = () => {
    // console.log('searchInfo: ', searchInfo.value);
    currentPageTableData.value = []
    
    // 获取搜索关键词并转为小写用于不区分大小写的搜索
    const searchTitle = searchInfo.value.title.toLowerCase()
    const searchName = searchInfo.value.name.toLowerCase()
    const searchAccount = searchInfo.value.account.toLowerCase()
    
    for (let index = 0; index < tableDataCopy.value.length; index++) {
        const item = tableDataCopy.value[index]
        
        // 如果搜索关键词不为空，则进行模糊搜索匹配
        if (searchTitle && !item.title.toLowerCase().includes(searchTitle)) continue
        if (searchName && !item.name.toLowerCase().includes(searchName)) continue
        if (searchAccount && !item.account.toLowerCase().includes(searchAccount)) continue
        if (searchInfo.value.scale && searchInfo.value.scale !== item.scale) continue
        if (searchInfo.value.condition && searchInfo.value.condition !== 1 && searchInfo.value.condition !== item.condition) continue
        
        currentPageTableData.value.push(item)
    }
    
    tableData.value = currentPageTableData.value
}
// 重置搜索信息
const reset = () => {
    searchInfo.value = { title: '',name: '',account: '',scale: '',condition: '' }
    search()
}
onMounted(() => {
    tableDataCopy.value = tableData.value
    currentPageTableData.value = tableData.value.slice(0, 10)
    searchResultUsers.value = users.value
    // 默认选中学校根节点
    if (schoolData.value && schoolData.value.length > 0) {
      selectedNode.value = schoolData.value[0]
    }
})
// 切换当前页面
const handleCurrentChange = (newPage) => {
  let start = (newPage - 1) * pageSize.value
  let end = start + pageSize.value
  currentPageTableData.value = tableData.value.slice(start, end)
}
// 每页条数切换时
const handleSizeChange = (newPageSize) => {
  currentPageTableData.value = tableData.value.slice(0, newPageSize)
  currentPage.value = 1
}
// 获取评测量表名称
const getScaleName = (scaleId) => {
  for (const scale of scales.value) {
    if (scale.value == scaleId) return scale.label
  }
}
// 打开添加评测的对话框
const openAddDialog = () => {
  addInfo.value = { title: '', name: '', account: '', scale: '', report: '', eva: '...' }
  rightValue.value = []
  selectUsers.value = []
  multipleTableRef.value?.clearSelection()
  addDialogVisible.value = true
}
// 打开添加评测的内部对话框
const openInnerAddDialog = () => {
  innerAddVisible.value = true
}
// 提交添加评测
const submitAdd = () => {
  if (!addInfo.value.title || addInfo.value.title == '') return ElMessage.error('请填写标题')
  if (!selectUsers.value || selectUsers.value.length == 0) return ElMessage.error('请选择用户')
  if (!rightValue.value || rightValue.value.length == 0) return ElMessage.error('请选择量表')
  // 添加评测
  for (const user of selectUsers.value) {
    for (const right of rightValue.value) {
      const id = Math.max(...tableData.value.map(item => item.id)) + 1
      const title = addInfo.value.title
      const name = user.name
      const account = user.account
      const scale = right
      const report = addInfo.value.report ? 1 : 0
      const eva = addInfo.value.eva
      const condition = 3
      const createdTime = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
      const complishTime = ''
      // 添加学校、专业和班级信息
      const school = '河南工业和信息化职业学院'
      const major = user.major
      const className = user.class
      const data = { id, title, name, account, scale, report, eva, condition, createdTime, complishTime, school, major, class: className }
      tableData.value.push(data)
    }
  }
  tableDataCopy.value = tableData.value
  search()
  ElMessage.success('添加成功')
  addDialogVisible.value = false
}
// 内部表格搜索 - 考虑当前选中的学校节点（支持四级结构）
const innerSearch = () => {
  if (!inputNameOrAccount.value || inputNameOrAccount.value == '') {
    // 如果搜索关键词为空，根据当前选中的节点筛选
    if (selectedNode.value) {
      handleNodeClick(selectedNode.value)
    } else {
      // 如果没有选中节点，显示所有学生
      searchResultUsers.value = users.value
    }
  } else {
    let filteredUsers = users.value
    
    // 先根据选中的节点筛选基础数据
    if (selectedNode.value) {
      if (selectedNode.value.label === '河南工业和信息化职业学院') {
        // 选中学校节点，不额外筛选
      } else if (selectedNode.value.label === '计算机应用技术') {
        // 选中专业节点，筛选该专业的学生
        filteredUsers = filteredUsers.filter(user => user.major === selectedNode.value.label)
      } else if (selectedNode.value.children) {
        // 选中年级节点，筛选该年级的学生
        filteredUsers = filteredUsers.filter(user => user.grade === selectedNode.value.label)
      } else {
        // 选中班级节点，需要找到父年级和父专业
        const gradeNode = findParentNode(schoolData.value, selectedNode.value)
        if (gradeNode) {
          const majorNode = findParentNode(schoolData.value, gradeNode)
          if (majorNode) {
            filteredUsers = filteredUsers.filter(user => 
              user.major === majorNode.label && 
              user.grade === gradeNode.label && 
              user.class === selectedNode.value.label
            )
          }
        }
      }
    }
    
    // 再根据搜索关键词筛选
    searchResultUsers.value = filteredUsers.filter(user => 
      user.name.includes(inputNameOrAccount.value) || user.account.includes(inputNameOrAccount.value)
    )
  }
}
// 内部表格选择用户变化
const handleSelectionUserChange = (data) => {
  selectUsers.value = data
}
// 删除评测
const deleteEva = () => {
  console.log('deleteId: ', deleteId.value);
  if (!deleteId.value) return ElMessage.error('请选择要删除的评测')
  tableData.value = tableData.value.filter(item => item.id != deleteId.value)
  tableDataCopy.value = tableData.value
  search()
  ElMessage.success('删除成功')
  deleteDialogVisible.value = false
}
// 打开编辑对话框
const openEditDialog = (id) => {
  for (const data of tableData.value) {
    if (data.id == id) {
      editInfo.value.id = id
      editInfo.value.title = data.title
      editInfo.value.scale = data.scale
      editInfo.value.report = data.report
      editInfo.value.condition = data.condition
    }
  }
  editDialogVisible.value = true
}
// 提交编辑
const submitEdit = () => {
  if (!editInfo.value.title || editInfo.value.title == '') return ElMessage.error('请填写标题')
  for (let index = 0; index < tableData.value.length; index++) {
    if (tableData.value[index].id == editInfo.value.id) {
      tableData.value[index].title = editInfo.value.title
      tableData.value[index].scale = editInfo.value.scale
      tableData.value[index].report = editInfo.value.report
      tableData.value[index].condition = editInfo.value.condition
      break
    }
  }
  tableDataCopy.value = tableData.value
  ElMessage.success('编辑成功')
  search()
  editDialogVisible.value = false
}
</script>

<template>
<div class="top1">
    <!-- 搜索栏1 -->
    <el-row>
        <el-col :span="4"><el-button type="primary" @click="openAddDialog"><el-icon><Plus /></el-icon>&nbsp;添加</el-button></el-col>
        <el-col :span="5"><el-input v-model="searchInfo.title" style="width: 240px" placeholder="请输入标题" /></el-col>
        <el-col :span="5"><el-input v-model="searchInfo.name" style="width: 240px" placeholder="请输入姓名" /></el-col>
        <el-col :span="5"><el-input v-model="searchInfo.account" style="width: 240px" placeholder="请输入账号" /></el-col>
        <el-col :span="5">
            <el-select v-model="searchInfo.scale" placeholder="选择量表" style="width: 240px">
                <el-option v-for="scale in scales" :key="scale.value" :label="scale.label" :value="scale.value"/>
            </el-select>
        </el-col>
    </el-row>
</div>
<div class="top2">
    <!-- 搜索栏2 -->
    <el-row>
        <el-col :span="4"></el-col>
        <el-col :span="5">
            <el-select v-model="searchInfo.condition" placeholder="状态" style="width: 240px">
                <el-option v-for="condition in conditions" :key="condition.value" :label="condition.label" :value="condition.value"/>
            </el-select>
        </el-col>
        <el-col :span="5">
            <el-button type="primary" @click="search"><el-icon><Search /></el-icon>&nbsp;&nbsp;查询</el-button>
            <el-button type="primary" @click="reset"><el-icon><Remove /></el-icon>&nbsp;&nbsp;重置</el-button>
        </el-col>
        <el-col :span="5">
        </el-col>
        <el-col :span="5"></el-col>
    </el-row>
</div>
<div class="table-box">
    <!-- 表格 -->
    <el-table :data="currentPageTableData" style="width: 100%" height="400" :header-cell-style="{ 'background-color': '#f5f7fa' }">
        <el-table-column prop="title" label="标题" width="250" />
        <el-table-column prop="name" label="姓名" width="100" />
        <el-table-column prop="account" label="账号" width="120" />
        <el-table-column label="测评量表" width="200" >
            <template #default="scope">
                {{ getScaleName(scope.row.scale) }}
            </template>
        </el-table-column>
        <el-table-column prop="report" label="开放报告" width="60" >
            <template #default="scope">
                {{ scope.row.report == 0 ? '否' : '是' }}
            </template>
        </el-table-column>
        <el-table-column label="状态" width="100" >
            <template #default="scope">
                <span :style="{ 'color': scope.row.condition == 2 ? '#606266' : 'red' }">{{ scope.row.condition == 2 ? '已完成' : '未完成' }}</span>
            </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="创建时间" width="160" />
        <el-table-column prop="complishTime" label="完成时间" width="160" />
        <el-table-column fixed="right" label="操作" min-width="120" >
            <template #default="scope">
                <el-button link type="primary" size="small" @click="openEditDialog(scope.row.id)">编辑</el-button>
                <el-button link type="danger" size="small" @click="deleteId =scope.row.id;deleteDialogVisible = true">删除</el-button>
            </template>
        </el-table-column>
  </el-table>
</div>
<!-- 放置分页 -->
<div class="page-box">
    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 50]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>
</div>
<!-- 添加评测的对话框 -->
 <el-dialog v-model="addDialogVisible" title="添加" width="920px">
    <!-- 添加评测的内部对话框 -->
    <el-dialog v-model="innerAddVisible" width="700px" title="测评对象" append-to-body>
      <!-- 放置学校分级 -->
      <div style="display: inline-block;width: 250px;height: 300px;border: 1px solid rgba(0, 0, 0, .1);">
        <el-scrollbar height="300px">
          <el-tree style="max-width: 600px" :data="schoolData" @node-click="handleNodeClick"/>
        </el-scrollbar>
      </div>
      <!-- 放置用户搜索框和表格 -->
      <div style="padding: 10px;margin-left: 10px; display: inline-block;width: 400px;height: 300px;border: 1px solid rgba(0, 0, 0, .1);">
        <!-- 搜索框 -->
        <el-input v-model="inputNameOrAccount" style="width: 308px" placeholder="输入姓名或账号搜索" />
        <el-button type="primary" style="margin-left: 10px;" @click="innerSearch">搜索</el-button>
        <!-- 内部表格 -->
        <el-table ref="multipleTableRef" :data="searchResultUsers" style="width: 100%;margin-top: 10px;" height="240px" 
          :header-cell-style="{ 'background-color': '#f5f7fa' }"
          @selection-change="handleSelectionUserChange">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="name" label="姓名" width="100" />
          <el-table-column prop="account" label="账号" width="120" />
          <el-table-column prop="major" label="专业" width="150" />
          <el-table-column prop="class" label="班级" width="70" />
        </el-table>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="innerAddVisible = false;multipleTableRef.clearSelection()">取消</el-button>
          <el-button type="primary" @click="innerAddVisible = false">确定</el-button>
        </div>
      </template>
    </el-dialog>
    <el-form :model="addInfo" >
      <!-- 添加标题 -->
      <el-form-item label="标题" :label-width="100" required>
        <el-input v-model="addInfo.title" autocomplete="off" style="width: 250px;" placeholder="请输入标题"/>
      </el-form-item>
      <!-- 添加测评对象 -->
      <el-form-item label="测评对象" :label-width="100" required>
        <el-button type="primary" @click="openInnerAddDialog">{{ selectUsers && selectUsers.length > 0 ? '已选择' : '未选择' }}</el-button>
      </el-form-item>
      <!-- 添加测评量表 -->
      <el-form-item label="测评量表" :label-width="100" required>
        <!-- 穿梭框 - 支持分组显示 -->
        <el-transfer v-model="rightValue" style="text-align: left; display: inline-block" filterable :titles="['量表库', '已选量表']"
          :format="{
            noChecked: '0/${total}',
            hasChecked: '${checked}/${total}',
          }"
          :data="scales"
          group-key="group"
          group-label="group">
          <template #left-empty>
            <el-empty :image-size="60" description="无数据" />
          </template>
          <template #right-empty>
            <el-empty :image-size="60" description="无数据" />
          </template>
        </el-transfer>
      </el-form-item>
      <!-- 选择开放报告 -->
      <el-form-item label="开放报告" :label-width="100" required>
        <el-switch v-model="addInfo.report" active-text="是" inactive-text="否"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitAdd">确定</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 删除数据的提示框 -->
  <el-dialog v-model="deleteDialogVisible" title="警告" width="500">
    <span>确定要删除此评测？</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="deleteEva">确定</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 编辑的对话框 -->
  <el-dialog v-model="editDialogVisible" title="编辑" width="500">
    <el-form :model="editInfo">
      <el-form-item label="标题" :label-width="80" required>
        <el-input v-model.trim="editInfo.title" autocomplete="off" />
      </el-form-item>
      <el-form-item label="测评量表" :label-width="80" required>
        <el-select v-model="editInfo.scale" placeholder="请选择量表" required>
          <el-option-group
            v-for="(groupedScales, groupName) in getGroupedScales()"
            :key="groupName"
            :label="groupName"
          >
            <el-option v-for="scale in groupedScales" :label="scale.label" :value="scale.value" :key="scale.value" />
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item label="开放报告" :label-width="80" required>
        <el-select v-model="editInfo.report" placeholder="是否开放报告" required>
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" :label-width="80">
        <el-select v-model="editInfo.condition" placeholder="选择状态">
          <el-option label="已完成" :value="2" />
          <el-option label="未完成" :value="3" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitEdit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
div {
    overflow: hidden;
}
.top1 {
    padding: 10px 0px 0px 10px;
    min-height: 50px;
}
.top2 {
    padding-left: 10px;
    min-height: 50px;
}
.table-box {
    padding-left: 10px;
}
.page-box {
    display: flex;
    justify-content: center;
    padding-left: 10px;
    margin-top: 10px;
}
/* 局部样式保持不变 */
</style>

<!-- 单独一个 <style> 标签，不加 scoped -->
<style>
/* 强制覆盖内联样式 */
.el-transfer-panel {
  width: 300px !important;
}
/* 左边：量表库 */
.el-transfer .el-transfer-panel:nth-child(1) {
  width: 300px !important;
}
/* 右边：已选量表 */
.el-transfer .el-transfer-panel:nth-child(2) {
  width: 400px !important;
}
</style>
